import os
import bottle
import subprocess
from bottle import route, run, template,request
@route('/alisa')
def index():
    params = bottle.request.params
    filename=params.get("filename", None)
    print(filename)
   # command = "python /home/wangqi/open_nsfw-master/classify_nsfw.py --model_def /home/wangqi/open_nsfw-master/nsfw_model/deploy.prototxt --pretrained_model /home/wangqi/open_nsfw-master/nsfw_model/resnet_50_1by2_nsfw.caffemodel /home/wangqi/open_nsfw-master/pic/"+filename
    command = "docker run --volume=$(pwd):/workspace caffe:cpu python ./classify_nsfw.py --model_def nsfw_model/deploy.prototxt --pretrained_model nsfw_model/resnet_50_1by2_nsfw.caffemodel 1.jpg"


    print(os.popen(command))
    # output = os.popen(command)
    

    return template('<b>{{text}}</b>', text=subprocess.getoutput(command))
    #return template('<b>{{text}}</b>', text="srgrsezg")


upload_path='static/'#定义上传文件的保存路径

@route('/upload')
def upload():
    return template('upload') #使用get方法会返回这个模版

@route('/upload',method='POST')
def do_upload():
    uploadfile=request.files.get('data') #获取上传的文件
    uploadfile.save(upload_path,overwrite=True)#overwrite参数是指覆盖同名文件
    command = "docker run --volume=$(pwd):/workspace caffe:cpu python ./classify_nsfw.py --model_def nsfw_model/deploy.prototxt --pretrained_model nsfw_model/resnet_50_1by2_nsfw.caffemodel "+upload_path+uploadfile.filename
    
    

    content=os.popen(command).read()

    return template('<b>{{text}}</b>', text= content)
    #return template('<b>{{text}}</b>', text=subprocess.getoutput(command))
    #return u"上传成功,文件名为：%s，文件类型为：%s"% (uploadfile.filename,uploadfile.content_type)
    #filename是获取上传文件文件名，content_type是获取上传的文件类型

run(host='0.0.0.0', port=8888)
